/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package collections;
import java.util.HashMap;
/**
 *
 * @author anonymous
 */
public class Recursion {
    private int arreglo [] = {1,2,3,4,5,6,7};
    private HashMap<Integer,Integer> fibonacci = new HashMap<>();
    
    public Recursion(){
            fibonacci.put(1, 0);
            fibonacci.put(2, 1);
    }
    public int McdCiclo( int p, int q) { 
        while (q != 0) {
            int temp = q;
            q = p % q;
            p = temp;
        }
        return p;
    }
   
    public int McdRecursivo( int p, int q) {        
        if (q == 0) return p;
        else return McdRecursivo(q, p % q);    
    }
    
    public int sumaV( int n) {
        if( n == 0) {
            return arreglo[n];
        } else 
            return arreglo[n] + sumaV(n-1);
    }
    
    //with cache
    public int fibCached( int n) {
        if( fibonacci.containsKey(n))
            return fibonacci.get(n);
        fibCached( n-1 );
        if( !fibonacci.containsKey(n))
            fibonacci.put(n,fibonacci.get(n-1) + fibonacci.get(n-2));
        return fibonacci.get(n);
    }
    
    //pure processing
    public int fib( int n) {
        if( n == 1 || n == 2 ) {
            return n-1;
        }
        return fib( n-1 ) + fib( n-2);
    }
    
    public int fibCicle( int n) {
        if( n == 1 || n == 2 ) {
            return n-1;
        }
        int n1 =0,n2=1,fib =1;
        for( int i =3; i<=n; i++ ) {
            fib = n1+n2;
            n1 = n2;
            n2 = fib;
        }
        return fib;
    }
  
    //hanoi towers
  public void move(int n, int startPole, int endPole) {
    if (n== 0){
      return; 
    }
    int intermediatePole = 6 - startPole - endPole;
    move(n-1, startPole, intermediatePole);
    System.out.println("Move " +n + " from " + startPole + " to " +endPole);
    move(n-1, intermediatePole, endPole);
  }
  
}
